package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Business.QiniuService;
import cn.edu.zut.cs.Business.SourcesService;
import cn.edu.zut.cs.Model.Sources;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class SourcesController {
    @Autowired
    QiniuService qiniuService;
    @Autowired
    SourcesService sourcesService;

    @RequestMapping("testupload")
    public String test(@RequestParam("use") String username,Model model,@RequestParam("pictureFile") MultipartFile file, HttpServletRequest request){
        //获取upload文件夹相对路径
        String url  = "/webapp/src/main/web/upload/";
        //使用UUID给图片重命名，并去掉四个 "-"
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        //获取文件的扩展名,后缀 .jpeg .png .jpg
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
      //  System.out.println(url+" "+"upload");
      //  System.out.println(name+" "+"重命名");
      //  System.out.println(ext +" "+"后缀");
       // System.out.println(url+name+"."+ext);
        try {
            byte[] bytes = file.getBytes();
            //上传到七牛云
            String urlpath=qiniuService.uploadBybyte(bytes,name+"."+ext);
            System.out.println(urlpath);
            //获取文件上传时间 格式 yyyy-MM-dd HH:mm:ss
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String uploadtime = simpleDateFormat.format(date);//上传时间
            System.out.println(uploadtime);
            String filetype="."+ext;//文件类型
            int i=sourcesService.insertupload(username,urlpath,filetype,uploadtime);//添加到Sources表中
            System.out.println(i);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Sources> sourcesList=sourcesService.querySourceBymyselfe(username);
        model.addAttribute("sourceslist",sourcesList);
        return "myroom";
    }

    @RequestMapping("/myroom")
    public String myroom(Model model,String myname){
        List<Sources> sourcesList=sourcesService.querySourceBymyselfe(myname);
        model.addAttribute("sourceslist",sourcesList);
        return "myroom";
    }

    @RequestMapping("uploadLog")
    public String mylogs(Model model, @RequestParam("use1") String username, @RequestParam("Log") String mylogs) {
        String filetype="日志";
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uploadtime = simpleDateFormat.format(date);//上传时间
        List<Sources> list = sourcesService.querySourceBymyselfe(username);
        int t=0;
        for (Sources a : list ){
            if (a.getFiletype().equals("日志")&&a.getUrlpath().equals(mylogs)){
                t=1;
                break;
            }
        }
        if (t!=1){
            int i=sourcesService.insertupload(username,mylogs,filetype,uploadtime);//添加到Sources表中
            List<Sources> sourcesList=sourcesService.querySourceBymyselfe(username);
            model.addAttribute("sourceslist",sourcesList);
            return "myroom";
        }
        List<Sources> sourcesList=sourcesService.querySourceBymyselfe(username);
        model.addAttribute("sourceslist",sourcesList);
        return "myroom";
    }
}
